home *** CD-ROM | disk | FTP | other *** search
/ Point Programming 1 / PPROG1.ISO / pascal / swag / printing.swg / 0026_PrintUsing in PASCAL.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-11-21  |  1.9 KB  |  58 lines

  1. {
  2. From: PHIL NICKELL
  3. Subj: Basic PrintUsing in PAS
  4. Does anyone know of any shareware or freeware routines in Turbo Pascal
  5. 5.5, that will allow me to format numbers or strings like the PRINTUSING
  6. statement in BASIC???
  7. }
  8.  
  9.  PROCEDURE printusing (mask: string; value:real);
  10.    { Calling syntax =     PRINTUSING(mask, number)
  11.      mask can be a string label or a literal
  12.      Example  printusing('#,###,###',45.63);
  13.               printusing('######.###,value);   }
  14.   const
  15.     comma     : char = ',';
  16.     point     : char = '.';
  17.     minussign : char = '-';
  18.   var
  19.     fieldwidth, integerlength, i, j, places, pointposition: integer;
  20.     usingcommas, decimal, negative : boolean;
  21.     outstring, integerstring : string;
  22.  
  23.   begin
  24.        negative := ( value < 0 );
  25.        value := abs( value );
  26.        places := 0;
  27.        fieldwidth := length( mask );
  28.        usingcommas := ( pos ( comma, mask ) > 0 );
  29.        decimal := ( pos (point,mask) > 0 );
  30.        if decimal then
  31.           begin
  32.                pointposition := pos(point, mask);
  33.                places := fieldwidth - pointposition;
  34.           END;
  35.        str ( value:0:places, outstring );
  36.        if usingcommas then
  37.           begin
  38.                J := 0;
  39.                integerstring :=
  40.                      copy (outstring, 1, length(outstring)-places);
  41.                integerlength := length(integerstring);
  42.                if decimal then
  43.                   integerlength := pred(integerlength);
  44.                for i := integerlength downto 2 do
  45.                    begin
  46.                         inc(j);
  47.                         if j mod 3 = 0 then
  48.                            insert (comma,outstring,i);
  49.                    end;
  50.           end;
  51.        if negative then
  52.                outstring := minussign + outstring;
  53.        write( outstring:fieldwidth);
  54.   END; {PRINTUSING}
  55.  
  56. BEGIN
  57. PrintUsing('##,###,###.##',123456.78);
  58. END.